package com.beiing.leafchartdemo.slice;


import com.beiing.leafchartdemo.ResourceTable;
import com.beiing.leafchat.SlideSelectLineChart;
import com.beiing.leafchat.bean.Axis;
import com.beiing.leafchat.bean.AxisValue;
import com.beiing.leafchat.bean.Line;
import com.beiing.leafchat.bean.PointValue;
import com.beiing.leafchat.bean.SlidingLine;
import com.beiing.leafchat.support.OnChartSelectedListener;
import com.beiing.leafchat.support.OnPointSelectListener;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

public class SlideSelectLineChartSlice extends AbilitySlice {

    SlideSelectLineChart slideSelectLineChart;
    Text tvSelectPoint;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_activity_move_select_line_chart);
        slideSelectLineChart = (SlideSelectLineChart) findComponentById(ResourceTable.Id_move_select_chart);
        tvSelectPoint = (Text) findComponentById(ResourceTable.Id_tv_select_point);

        initLineChart();

        slideSelectLineChart.setOnPointSelectListener(new OnPointSelectListener() {
            @Override
            public void onPointSelect(int position, String xLabel, String value) {
                String point = xLabel + ":" + value;
                tvSelectPoint.setText(point);
            }
        });

        slideSelectLineChart.setOnChartSelectedListener(new OnChartSelectedListener() {
            @Override
            public void onChartSelected(boolean isChartSelected) {

                System.out.println("====isChartSelected: " + isChartSelected);
            }
        });
    }


    private void initLineChart() {
        Axis axisX = new Axis(getAxisValuesX());
        axisX.setAxisColor(RgbPalette.parse("#33B5E5")).setTextColor(Color.DKGRAY.getValue()).setHasLines(false).setShowText(false);
        Axis axisY = new Axis(getAxisValuesY());
        axisY.setAxisColor(RgbPalette.parse("#33B5E5")).setTextColor(Color.DKGRAY.getValue()).setHasLines(false).setShowText(true);
        slideSelectLineChart.setAxisX(axisX);
        slideSelectLineChart.setAxisY(axisY);

        slideSelectLineChart.setSlideLine(getSlideingLine());
        slideSelectLineChart.setChartData(getFoldLine());
        slideSelectLineChart.show();
    }


    int days = 90;

    private List<AxisValue> getAxisValuesX() {
        List<AxisValue> axisValues = new ArrayList<>();
        for (int i = 1; i <= days; i++) {
            AxisValue value = new AxisValue();
            value.setLabel(i + "日");
            axisValues.add(value);
        }
        return axisValues;
    }

    private List<AxisValue> getAxisValuesY() {
        List<AxisValue> axisValues = new ArrayList<>();
        for (int i = 0; i < 11; i++) {
            AxisValue value = new AxisValue();
            value.setLabel(String.valueOf(i * 10));
            axisValues.add(value);
        }
        return axisValues;
    }

    private Line getFoldLine() {
        List<PointValue> pointValues = new ArrayList<>();
        SecureRandom random = new SecureRandom();
        for (int i = 1; i <= days; i++) {
            PointValue pointValue = new PointValue();
            pointValue.setX((float) (i - 1) / (days - 1));
            int var = 5 + i + (int) (random.nextFloat() * 10);
            pointValue.setLabel(String.valueOf(var));
            pointValue.setY(var / 100f);
            pointValues.add(pointValue);
        }

        Line line = new Line(pointValues);
        line.setLineColor(RgbPalette.parse("#33B5E5"))
                .setLineWidth(1.5f)
                .setPointColor(RgbPalette.parse("#33B5E5"))
                .setCubic(true)
                .setPointRadius(2)
                .setFill(true)
                .setHasPoints(true)
                .setFillColor(RgbPalette.parse("#33B5E5"))
                .setLabelColor(RgbPalette.parse("#33B5E5"))
                .setHasLabels(true);
        return line;
    }

    private SlidingLine getSlideingLine() {
        SlidingLine slidingLine = new SlidingLine();
        slidingLine.setSlideLineColor(RgbPalette.parse("#FF4081"))
                .setSlidePointColor(RgbPalette.parse("#FF4081"))
                .setSlidePointRadius(5);
        return slidingLine;
    }
}
